//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.HealthCheck
{
    using System;
    using System.Threading;
    using Microsoft.Research.DataLayer;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;

    public class HealthCheckService
    {
        /// <summary>
        /// Handle to the registry.
        /// </summary>
        private Connection registryHandle = null;

        public HealthCheckService(Connection connection)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }
            this.registryHandle = connection;
            this.registryHandle.Open();
        }

        /// <summary>
        /// Gets or sets the client refresh time.
        /// </summary>
        /// <value>The client refresh time.</value>
        public int ClientRefreshTime { get; set; }

        /// <summary>
        /// Gets or sets the heart beat timeout.
        /// </summary>
        /// <value>The heart beat timeout.</value>
        public int HeartbeatTimeout { get; set; }

        /// <summary>
        /// Occurs when [health check event].
        /// </summary>
        public event HealthCheckEventHandler HealthCheckEvent;

        /// <summary>
        /// Health check base class.
        /// </summary>
        HealthCheckBase healthCheck;

        /// <summary>
        /// Performs the health check.
        /// </summary>
        public void PerformHealthCheck()
        {
            while (true)
            {
                try
                {
                    // Execution service health check.
                    healthCheck = new ExecutionServiceHealthCheck(this.HeartbeatTimeout);
                    (healthCheck as ExecutionServiceHealthCheck).ExecutionServiceHealthCheckEvent += 
                        new ExecutionServiceHealthCheckEventHandler(HealthCheckService_ExecutionServiceHealthCheckEvent);
                    healthCheck.PerformHealthCheck(this.registryHandle);

                    Thread.Sleep(ClientRefreshTime * 1000);
                }
                catch (ThreadAbortException exp)
                {
                    TridentErrorHandler.HandleUIException(exp);
                    return;
                }
                catch (Exception exp)
                {
                    TridentErrorHandler.HandleUIException(exp);
                    break;
                }
            }
        }

        /// <summary>
        /// Handles the ExecutionServiceHealthCheckEvent event of the HealthCheckService control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Microsoft.Research.ScientificWorkflow.ManagementStudioControls.HealthCheck.HealthCheckEventArgs"/> instance containing the event data.</param>
        void HealthCheckService_ExecutionServiceHealthCheckEvent(object sender, HealthCheckEventArgs e)
        {
            if (HealthCheckEvent != null)
            {
                HealthCheckEvent(sender, e);
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public delegate void HealthCheckEventHandler(object sender, HealthCheckEventArgs e);
}
